package com.hundsun.yr.universal.p2p.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseAppManager;
import com.hundsun.yr.universal.library.base.BaseWebActivity;
import com.hundsun.yr.universal.library.eventbus.EventCenter;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.utils.RegularUtils;
import com.hundsun.yr.universal.library.widget.HSButton;
import com.hundsun.yr.universal.library.widget.HSEditText;
import com.hundsun.yr.universal.p2p.bean.UpdatePayPwdBeen;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.presenter.base.RequestPresenter;
import com.hundsun.yr.universal.p2p.presenter.impl.InitializePresenterImpl;
import com.hundsun.yr.universal.p2p.ui.activity.base.BaseActivity;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.CommTool;
import com.hundsun.yr.universal.p2p.view.base.InitializeView;
import com.hundsun.yr.universal.p2p.widget.ValidateButton;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 * Created by xx on 2017/5/31.
 * 修改交易密码
 */
public class UpdatePayPwdActivity extends BaseActivity implements View.OnClickListener,InitializeView{
    private TextView tv_phone;
    private HSEditText et_vcode;
    private ValidateButton btn_vcode;
    private HSButton btn_update_pay_pwd;
    private String phone = "";
    private String title = "修改交易密码";
    private String vCode = "";
    private RequestPresenter mPresenter;
    private String zwgt_data;

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_update_pay_pwd;
    }

    @Override
    protected void initViewsAndEvents() {
        findview();
        setToolbarTitle(title);
        tv_phone.setText(getString(R.string.send_vcode_to_phone,CommTool.encodPhone(sharedPreferences.getShareProf(UserInfo.BANKMOBILE))));
        btn_vcode.setOnClickListener(this);
        btn_update_pay_pwd.setOnClickListener(this);
        mPresenter = new InitializePresenterImpl(mContext,this);

    }

    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.titleBar(R.id.common_toolbar).init();
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        phone = extras.getString("phone");
        title = extras.getString("title");
    }

    @Override
    protected void onEveComming(EventCenter eventCenter) {

    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected View getLoadingTargetView() {
        return null;
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return true;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    public void findview(){
        tv_phone = findView(R.id.tv_phone);
        et_vcode = findView(R.id.et_vcode);
        btn_vcode = findView(R.id.btn_vcode);
        btn_update_pay_pwd = findView(R.id.btn_update_pay_pwd);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_vcode://验证码
                phone = sharedPreferences.getShareProf(UserInfo.BANKMOBILE);

                if (TextUtils.isEmpty(phone)){
                    showToast(R.string.empty_phone_number);
                    return;
                }
                //检查手机号格式
                if (!RegularUtils.isPhoneNum(phone)) {
                    showToast(R.string.error_phone_number);
                    return;
                }

                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map = new JSONObject();

                try {
                    map.put("srvtxcode", "passwordResetPlus");
                    map.put("phone", phone);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                JSONObject jsonParams = CommTool.getJSONParams(map, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
                mPresenter.loadingData(TaskConstants.TASK_SMS_APPLY, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_pub_test) + TaskConstants.TASK_SMS_APPLY, Constants.EVENT_DATA,
                        jsonParams, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_pub_test) + TaskConstants.TASK_SMS_APPLY, sharedPreferences
                                .getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data),true);
                break;
            case R.id.btn_update_pay_pwd://修改交易密码
                //开始修改交易密码
                vCode = et_vcode.getText().toString();

                if (TextUtils.isEmpty(vCode)){
                    showToast(R.string.empty_vcode);
                    return;
                }
                //检查手机号格式
                if (!RegularUtils.isLength(vCode,6)) {
                    showToast(R.string.error_vcode);
                    return;
                }

                zwgt_data = BaseRequestTool2.getFormatedDateString();
                JSONObject map1 = new JSONObject();

                try {
                    map1.put("vcode", vCode);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                JSONObject jsonParams1 = CommTool.getJSONParams(map1, sharedPreferences.getShareProf(UserInfo.TOKENID).substring(0, 24), zwgt_data);
                mPresenter.loadingData(TaskConstants.TASK_SETBANKPWD, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_user_test) + TaskConstants.TASK_SETBANKPWD, Constants.EVENT_DATA,
                        jsonParams1, BaseRequestTool2.getHearder(mContext, TaskConstants.TASK_POST, mContext.getString(R.string.api_user_test) + TaskConstants.TASK_SETBANKPWD, sharedPreferences
                                .getShareProf(UserInfo.USER_NO), sharedPreferences.getShareProf(UserInfo.TOKENID), zwgt_data),true);
                break;
        }
    }

    @Override
    public <T> void initializeViews(String task_name, T data) {
        switch (task_name){
            case TaskConstants.TASK_SETBANKPWD://修改交易密码  "url": "https://test.credit2go.cn/escrow/p2p/page/mobile/plus"
                if (data == null){
                    return;
                }

                UpdatePayPwdBeen mUpdatePayPwdBeen = (UpdatePayPwdBeen) data;

                //现在做另外一种操作
                String postData = null;
                try {
                    postData = "accountId=" + URLEncoder.encode(mUpdatePayPwdBeen.data.accountId.toString(), "utf-8") + "&acqRes=" + URLEncoder.encode(mUpdatePayPwdBeen.data.acqRes.toString(),"utf-8") + "&bankCode=" + URLEncoder.encode(mUpdatePayPwdBeen.data.bankCode.toString(),"utf-8")
                            + "&channel=" + URLEncoder.encode(mUpdatePayPwdBeen.data.channel.toString(), "utf-8") + "&idNo=" + URLEncoder.encode(mUpdatePayPwdBeen.data.idNo.toString(),"utf-8") + "&idType=" + URLEncoder.encode(mUpdatePayPwdBeen.data.idType.toString(),"utf-8") + "&instCode=" + URLEncoder.encode(mUpdatePayPwdBeen.data.instCode.toString(),"utf-8") + "&lastSrvAuthCode="
                            + URLEncoder.encode(mUpdatePayPwdBeen.data.lastSrvAuthCode.toString(),"utf-8") + "&mobile=" + URLEncoder.encode(mUpdatePayPwdBeen.data.mobile.toString(),"utf-8") + "&name=" + URLEncoder.encode(mUpdatePayPwdBeen.data.name.toString(),"utf-8") + "&notifyUrl=" + URLEncoder.encode(mUpdatePayPwdBeen.data.notifyUrl.toString(),"utf-8")
                            +"&retUrl=" + URLEncoder.encode(mUpdatePayPwdBeen.data.retUrl.toString(),"utf-8") + "&seqNo=" + URLEncoder.encode(mUpdatePayPwdBeen.data.seqNo.toString(),"utf-8") + "&sign=" + URLEncoder.encode(mUpdatePayPwdBeen.data.sign.toString(),"utf-8") + "&smsCode=" + URLEncoder.encode(mUpdatePayPwdBeen.data.smsCode.toString(),"utf-8")
                            + "&txCode=" + URLEncoder.encode(mUpdatePayPwdBeen.data.txCode.toString(),"utf-8") + "&txDate=" + URLEncoder.encode(mUpdatePayPwdBeen.data.txDate.toString(),"utf-8") + "&txTime=" + URLEncoder.encode(mUpdatePayPwdBeen.data.txTime.toString(),"utf-8") + "&version=" + URLEncoder.encode(mUpdatePayPwdBeen.data.version.toString(),"utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

                Bundle bundle = new Bundle();
                bundle.putString(BaseWebActivity.BUNDLE_KEY_TITLE, getString(R.string.update_trading_pass));
                bundle.putString(BaseWebActivity.BUNDLE_KEY_URL,mUpdatePayPwdBeen.url);
                bundle.putString(BaseWebActivity.BUNDLE_KEY_PARAMS,postData);
                bundle.putInt(BaseWebActivity.BUNDLE_KEY_LOADTYPE, BaseWebActivity.LoadType.POSTDATA.getIntValue());
                bundle.putBoolean(BaseWebActivity.BUNDLE_KEY_SHOW_BOTTOM_BAR, false);
                readyGoThenKill(WebSetPasswordActivity.class, bundle);
                if (BaseAppManager.getInstance().getActivity(MineSettingActivity.class) != null){
                    BaseAppManager.getInstance().getActivity(MineSettingActivity.class).finish();
                }
                break;
        }
    }

    @Override
    public <T> void initializeError(String task_name, T data) {

    }
}
